package com.itheima.springbootwebtlias.service.Impl;

import com.itheima.springbootwebtlias.anno.LogOperation;
import com.itheima.springbootwebtlias.exception.DeletionIsNotAllowed;
import com.itheima.springbootwebtlias.mapper.DeptMapper;
import com.itheima.springbootwebtlias.mapper.EmpMapper;
import com.itheima.springbootwebtlias.pojo.Dept;
import com.itheima.springbootwebtlias.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门业务层实现类：处理部门业务逻辑
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> selectAll() {
        return deptMapper.selectAllDept();
    }

    @LogOperation
    @Override
    public void removeById(Integer id) {
        Integer count = empMapper.selectByDeptId(id);

        if(count != 0){
            throw new DeletionIsNotAllowed("当前部门存在员工，不允许删除");
        }

        deptMapper.removeById(id);
    }

    @LogOperation
    @Override
    public void addDept(Dept dept) {
//        LocalDateTime now = LocalDateTime.now();
//        LocalDateTime now2 = LocalDateTime.now();


        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
    }

    @Override
    public Dept selectById(Integer id) {


        return deptMapper.selectById(id);
    }

    @LogOperation
    @Override
    public void updateById(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }


}
